package com.leo.lime.music;

import android.os.Bundle;

import android.support.v4.view.GravityCompat;
import android.view.View;
import android.view.Window;
import android.graphics.Color;
import android.view.WindowManager;

import android.support.v7.widget.Toolbar;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;

import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.leo.lime.music.controller.SharedPrefController;
import com.leo.lime.music.fragment.ContentFragment;
import com.leo.lime.music.fragment.DrawerFragment;
import com.leo.lime.music.view.DrawableTextView;

import java.util.ArrayList;

public class Main extends AppCompatActivity implements ContentFragment.PagerChangedListener {
    private static final String TAG = "Main";

    public static final int PAGER_ME = 0;
    public static final int PAGER_LISTENER = 1;
    public static final int PAGER_LOOK = 2;
    public static final int PAGER_SING = 3;

    private DrawerLayout mMainDrawerLayout;
    private ArrayList<Listener> mListeners;

    private ImageView mToolbarDrawerIv;
    private ImageView mToolbarRecordIv;
    private LinearLayout mContentTabsLl;
    private TextView mContentTabMeTv;
    private TextView mContentTabListenerTv;
    private TextView mContentTabLookTv;
    private TextView mContentTabSingTv;

    private RelativeLayout mToolBarSearchViewRl;
    private DrawableTextView mToolbarSearchTv;

    private LinearLayout mToolbarLookTabLl;
    private TextView mLiveBroadcastTabTv;
    private TextView mShortVideoTabTv;
    private TextView mMvTabTv;

    private DrawerLayout.SimpleDrawerListener mDrawerListener = new DrawerLayout.SimpleDrawerListener() {
        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
            if (mListeners != null && mListeners.size() > 0) {
                for (Listener listener : mListeners) {
                    listener.onDrawerSlide(drawerView, slideOffset);
                }
            }
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            if (mListeners != null && mListeners.size() > 0) {
                for (Listener listener : mListeners) {
                    listener.onDrawerOpened(drawerView);
                }
            }
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            if (mListeners != null && mListeners.size() > 0) {
                for (Listener listener : mListeners) {
                    listener.onDrawerClosed(drawerView);
                }
            }
        }

        @Override
        public void onDrawerStateChanged(int newState) {
            if (mListeners != null && mListeners.size() > 0) {
                for (Listener listener : mListeners) {
                    listener.onDrawerStateChanged(newState);
                }
            }
        }
    };

    private View.OnClickListener mToolbarClickListener = new View.OnClickListener() {
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.toolbar_drawer_iv:
                    onToolbarDrawerIvClick();
                    break;
                case R.id.content_record_iv:
                    onToolbarRecordIvClick(view);
                    break;
                case R.id.content_tab_me:
                    onContentMeTabClick(view);
                    break;
                case R.id.content_tab_listener:
                    onContentListenerTabClick(view);
                    break;
                case R.id.content_tab_look:
                    onContentLookTabClick(view);
                    break;
                case R.id.content_tab_sing:
                    onContentSingTabClick(view);
                    break;
                case R.id.main_drawer_searchview:
                    onSearchViewClick(view);
                    break;
                case R.id.main_drawer_toolbar_look_tabs_livebraodcast:
                    onLiveBroadcaseTabClick(view);
                    break;
                case R.id.main_drawer_toolbar_look_tabs_video:
                    onVideoTabClick(view);
                    break;
                case R.id.main_drawer_toolbar_look_tabs_mv:
                    onMVTabClick(view);
                    break;
            }
        }
    };

    public interface Listener {
        void onDrawerSlide(View drawerView, float slideOffset);

        void onDrawerOpened(View drawerView);

        void onDrawerClosed(View drawerView);

        void onDrawerStateChanged(int newState);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setStatusBarColors(R.color.status_bar_background, Color.WHITE);
        setContentView(R.layout.main);

        initailToolbarViews();

        getSupportFragmentManager()
                .beginTransaction()
                .replace(
                        R.id.content_view, new ContentFragment()
                )
                .replace(
                        R.id.main_left_drawer, new DrawerFragment()
                )
                .commit();
    }

    private void setStatusBarColors(int statusColor, int navigationColor) {
        Window window = getWindow();
        window.clearFlags(
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS |
                        WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
        );
        window.getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                        View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
                        View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        );
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(getResources().getColor(statusColor));
        window.setNavigationBarColor(navigationColor);
    }

    private void initailToolbarViews() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.content_action_bar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        mMainDrawerLayout = (DrawerLayout) findViewById(R.id.main_drawer_layout);
        mMainDrawerLayout.addDrawerListener(mDrawerListener);

        mToolbarDrawerIv = (ImageView) findViewById(R.id.toolbar_drawer_iv);
        mToolbarDrawerIv.setOnClickListener(mToolbarClickListener);
        mToolbarRecordIv = (ImageView) findViewById(R.id.content_record_iv);
        mToolbarRecordIv.setOnClickListener(mToolbarClickListener);

        mContentTabsLl = (LinearLayout) findViewById(R.id.content_tabs);
        mContentTabMeTv = (TextView) findViewById(R.id.content_tab_me);
        mContentTabMeTv.setOnClickListener(mToolbarClickListener);
        mContentTabListenerTv = (TextView) findViewById(R.id.content_tab_listener);
        mContentTabListenerTv.setOnClickListener(mToolbarClickListener);
        mContentTabLookTv = (TextView) findViewById(R.id.content_tab_look);
        mContentTabLookTv.setOnClickListener(mToolbarClickListener);
        mContentTabSingTv = (TextView) findViewById(R.id.content_tab_sing);
        mContentTabSingTv.setOnClickListener(mToolbarClickListener);

        mToolBarSearchViewRl = (RelativeLayout) findViewById(R.id.main_drawer_searchview);
        mToolbarSearchTv = (DrawableTextView) findViewById(R.id.main_drawer_searchview_tv);
        mToolBarSearchViewRl.setOnClickListener(mToolbarClickListener);

        mToolbarLookTabLl = (LinearLayout) findViewById(R.id.main_drawer_toolbar_look_tabs);
        mLiveBroadcastTabTv = (TextView) findViewById(R.id.main_drawer_toolbar_look_tabs_livebraodcast);
        mLiveBroadcastTabTv.setOnClickListener(mToolbarClickListener);
        mShortVideoTabTv = (TextView) findViewById(R.id.main_drawer_toolbar_look_tabs_video);
        mShortVideoTabTv.setOnClickListener(mToolbarClickListener);
        mMvTabTv = (TextView) findViewById(R.id.main_drawer_toolbar_look_tabs_mv);
        mMvTabTv.setOnClickListener(mToolbarClickListener);
    }

    @Override
    protected void onStart() {
        super.onStart();

        int index = SharedPrefController.getInstance(this).getContentPagerIndex();
        setPagerUi(index);
    }

    public void addDrawerListener(Listener listener) {
        if (mListeners == null) {
            mListeners = new ArrayList<Listener>();
        }
        if (!mListeners.contains(listener)) {
            mListeners.add(listener);
        }
    }

    public void removeAllDrawerListeners() {
        if (mListeners != null && mListeners.size() > 0) {
            mListeners.clear();
        }
    }

    public void removeDrawerListener(Listener listener) {
        if (mListeners != null && mListeners.size() > 0) {
            if (!mListeners.contains(listener)) {
                mListeners.remove(listener);
            }
        }
    }

    public void onPagerChangedListener(int index) {
        setPagerUi(index);
    }

    private void setPagerUi(int index) {
        int meTextSize = 20;
        int meTextColor = R.color.toolbar_vp_unsel_text_color;
        int listenerTextSize = 20;
        int listenerTextColor = R.color.toolbar_vp_unsel_text_color;
        int lookTextSize = 20;
        int lookTextColor = R.color.toolbar_vp_unsel_text_color;
        int singTextSize = 20;
        int singTextColor = R.color.toolbar_vp_unsel_text_color;
        boolean showSearchView = false;
        boolean showLookTabView = false;
        switch (index) {
            case PAGER_ME:
                meTextSize = 24;
                meTextColor = R.color.toolbar_vp_sel_text_color;
                showSearchView = true;
                break;
            case PAGER_LISTENER:
                listenerTextSize = 24;
                listenerTextColor = R.color.toolbar_vp_sel_text_color;
                showSearchView = true;
                break;
            case PAGER_LOOK:
                lookTextSize = 24;
                lookTextColor = R.color.toolbar_vp_sel_text_color;
                showLookTabView = true;
                break;
            case PAGER_SING:
                singTextSize = 24;
                singTextColor = R.color.toolbar_vp_sel_text_color;
                showSearchView = true;
                break;
        }
        mContentTabMeTv.setTextSize(meTextSize);
        mContentTabMeTv.setTextColor(getResources().getColor(meTextColor));
        mContentTabListenerTv.setTextSize(listenerTextSize);
        mContentTabListenerTv.setTextColor(getResources().getColor(listenerTextColor));
        mContentTabLookTv.setTextSize(lookTextSize);
        mContentTabLookTv.setTextColor(getResources().getColor(lookTextColor));
        mContentTabSingTv.setTextSize(singTextSize);
        mContentTabSingTv.setTextColor(getResources().getColor(singTextColor));

        if(showSearchView) {
            mToolbarLookTabLl.setVisibility(View.GONE);
            mToolBarSearchViewRl.setVisibility(View.VISIBLE);
            if(PAGER_SING == index) {
                mToolbarSearchTv.setText(R.string.main_drawer_searchview_hint_sing);
            } else {
                mToolbarSearchTv.setText(R.string.main_drawer_searchview_hint);
            }
        } else if(showLookTabView) {
            mToolBarSearchViewRl.setVisibility(View.GONE);
            mToolbarLookTabLl.setVisibility(View.VISIBLE);
        }

        SharedPrefController.getInstance(this).setContentPagerIndex(index);
    }

    private void onToolbarDrawerIvClick() {
        if (!mMainDrawerLayout.isDrawerOpen(GravityCompat.START)) {
            mMainDrawerLayout.openDrawer(GravityCompat.START, true);
        }
    }

    private void onToolbarRecordIvClick(View view) {}
    private void onContentMeTabClick(View view) {}
    private void onContentListenerTabClick(View view) {}
    private void onContentLookTabClick(View view) {}
    private void onContentSingTabClick(View view) {}
    private void onSearchViewClick(View view) {}
    private void onLiveBroadcaseTabClick(View view) {}
    private void onVideoTabClick(View view) {}
    private void onMVTabClick(View view) {}

    @Override
    protected void onDestroy() {
        SharedPrefController.getInstance(this).setContentPagerIndex(0);
        super.onDestroy();
    }
}
